﻿using Basic.WxApiService.Interface;
using WeDonekRpc.CacheClient.Interface;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Helper;
namespace Basic.WxApiService.WxPlaform
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class WxVerifyTicketService : IWxVerifyTicketService
    {
        private readonly IRedisCache _Cache;
        public WxVerifyTicketService (IRedisCache cache)
        {
            this._Cache = cache;
        }
        public void SetVerifyTicket (string appId, string verifyTicket)
        {
            string name = string.Concat("VerifyTicket_", appId);
            if (!this._Cache.Set(name, verifyTicket))
            {
                throw new ErrorException("wx.platform.verifyTicket.set.fail");
            }
        }
        public string GetVerifyTicket (string appId)
        {
            string name = string.Concat("VerifyTicket_", appId);
            if (!this._Cache.TryGet(name, out string ticket))
            {
                throw new ErrorException("wx.platform.verifyTicket.null");
            }
            return ticket;
        }
    }
}
